<?

	function func1($id) 
	// returns user data of passed $id
	// returns null if there is no id like $id
	{
		$query = "select * from users where id= '".$id."'";
		$result = mysql_query($query);
		$num_results = mysql_num_rows($result);

		if($num_results ==0){
			$row = null;
		}
		else {
			$row = mysql_fetch_array($result);
		}
		return $row;
	} // end of function1 


function func2($where_string)
	// returns number of result 
{
	if ($where_string!="") 
		$query = "select * from keywords natural join users where ".$where_string.";";
	else
		$query = "select * from keywords natural join users";

	$result = mysql_query($query);
	$num_results = mysql_num_rows($result);

		
	for($i=0;$i<$num_results;$i++){
		$ret["$i"] = mysql_fetch_array($result);
	}
	$ret["num_results"] = $num_results;
	return $ret;
}

function func3($keyword)
{
	$query = "select * from keywords natural join users where keyword like '%".$keyword."%';";
	$result = mysql_query($query);
	$num_results = mysql_num_rows($result);

		
	for($i=0;$i<$num_results;$i++){
		$ret["$i"] = mysql_fetch_array($result);
	}
	$ret["num_results"] = $num_results;
	return $ret;
}


function connectDB(){

// db connection

	$db = mysql_pconnect("localhost", "palhunt", "palhunt");
	if (!$db){
		echo "Error : Could not connect to database."; 
		exit;
	}
	mysql_select_db("PalHunt");
// end of db connection

}

connectDB();

/*
echo "use example for first function<br>";
$example1= func1("ssukmaec@gmail.com");
echo $example1["id"]."<br>";
echo $example1["password"]."<br>";
echo $example1["name"]."<br>";
echo $example1["age"]."<br>";
echo $example1["keyword"]."<br>";

echo "<br>use example for second function<br>";
$example2= func2("id='ssukmaec@gmail.com'");
for($i=0;$i<$example2["num_results"];$i++){
	echo $example2["$i"]["id"]."<br>";
	echo $example2["$i"]["password"]."<br>";
	echo $example2["$i"]["name"]."<br>";
	echo $example2["$i"]["age"]."<br>";
	echo $example2["$i"]["keyword"]."<br>";
}

echo "<br>use example for third function<br>";
$example3= func3("computer");
for($i=0;$i<$example3["num_results"];$i++){
	echo $example3["$i"]["id"]."<br>";
	echo $example3["$i"]["password"]."<br>";
	echo $example3["$i"]["name"]."<br>";
	echo $example3["$i"]["age"]."<br>";
	echo $example3["$i"]["keyword"]."<br>";
}
*/
?>

